package pe.edu.upc.dsd.epica.movil;

import java.io.IOException;

import pe.edu.upc.dsd.epica.util.ConstantsTwitter;
import pe.edu.upc.dsd.epica.util.CredentialStore;
import pe.edu.upc.dsd.epica.util.QueryStringParser;
import pe.edu.upc.dsd.epica.util.SharedPreferencesCredentialStore;

import com.google.api.client.auth.oauth.OAuthAuthorizeTemporaryTokenUrl;
import com.google.api.client.auth.oauth.OAuthCredentialsResponse;
import com.google.api.client.auth.oauth.OAuthGetAccessToken;
import com.google.api.client.auth.oauth.OAuthGetTemporaryToken;
import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.http.apache.ApacheHttpTransport;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class OAuthTwitterActivity extends Activity {
	final String TAG = getClass().getName();

	private SharedPreferences prefs;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG, "Starting task to retrieve request token.");
		this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		WebView webview = new WebView(this);
		webview.getSettings().setJavaScriptEnabled(true);
		webview.setVisibility(View.VISIBLE);
		setContentView(webview);

		Log.i(TAG, "Retrieving request token from Google servers");

		try {

			final OAuthHmacSigner signer = new OAuthHmacSigner();
			signer.clientSharedSecret = ConstantsTwitter.CONSUMER_SECRET;

			OAuthGetTemporaryToken temporaryToken = new OAuthGetTemporaryToken(
					ConstantsTwitter.REQUEST_URL);
			temporaryToken.transport = new ApacheHttpTransport();
			temporaryToken.signer = signer;
			temporaryToken.consumerKey = ConstantsTwitter.CONSUMER_KEY;
			temporaryToken.callback = ConstantsTwitter.OAUTH_CALLBACK_URL;

			OAuthCredentialsResponse tempCredentials = temporaryToken.execute();
			signer.tokenSharedSecret = tempCredentials.tokenSecret;

			OAuthAuthorizeTemporaryTokenUrl authorizeUrl = new OAuthAuthorizeTemporaryTokenUrl(
					ConstantsTwitter.AUTHORIZE_URL);
			authorizeUrl.temporaryToken = tempCredentials.token;
			String authorizationUrl = authorizeUrl.build();

			/* WebViewClient must be set BEFORE calling loadUrl! */
			webview.setWebViewClient(new WebViewClient() {

				@Override
				public void onPageStarted(WebView view, String url,
						Bitmap bitmap) {
				}

				@Override
				public void onPageFinished(WebView view, String url) {

					if (url.toString().startsWith(ConstantsTwitter.OAUTH_CALLBACK_URL)) {
						try {

							if (url.toString().indexOf("oauth_token=") != -1) {

								String requestToken	= extractParamFromUrl(url, "oauth_token");
								String verifier = extractParamFromUrl(url, "oauth_verifier");

								signer.clientSharedSecret = ConstantsTwitter.CONSUMER_SECRET;

								OAuthGetAccessToken accessToken = new OAuthGetAccessToken(ConstantsTwitter.ACCESS_URL);

								accessToken.transport = new ApacheHttpTransport();

								accessToken.temporaryToken = requestToken;
								accessToken.signer	= signer;

								accessToken.consumerKey = ConstantsTwitter.CONSUMER_KEY;

								accessToken.verifier = verifier;

								OAuthCredentialsResponse credentials = accessToken.execute();

								signer.tokenSharedSecret = credentials.tokenSecret;

								CredentialStore	credentialStore = 
										new SharedPreferencesCredentialStore(prefs);
								String token =	credentials.token;
								String tokenSecret	= credentials.tokenSecret;
								
								SharedPreferencesCredentialStore.TOKEN = token;
								SharedPreferencesCredentialStore.TOKEN_SECRET = tokenSecret;

								credentialStore.write(new String[] { token,	tokenSecret });

								view.setVisibility(View.INVISIBLE);
								Intent intent = new	Intent(OAuthTwitterActivity.this,LoginActivity.class);
								intent.putExtra("token", token);
								intent.putExtra("tokenSecret", tokenSecret);
								startActivity(intent);
							} else if (url.indexOf("error=") != -1) {
								view.setVisibility(View.INVISIBLE);
								new SharedPreferencesCredentialStore(prefs).clearCredentials();
								startActivity(new Intent(OAuthTwitterActivity.this,epica.class));
							}

						} catch (IOException e) {
							e.printStackTrace();
						}

					}
				}

				private String extractParamFromUrl(String url, String paramName) {
					String queryString = url.substring(url.indexOf("?", 0) + 1,
							url.length());
					QueryStringParser queryStringParser = new QueryStringParser(
							queryString);
					return queryStringParser.getQueryParamValue(paramName);
				}

			});

			webview.loadUrl(authorizationUrl);
		} catch (Exception ex) {
			Log.e(TAG, ex.getMessage());
		}
	}
}
